import time
import urllib3
import json
import os
#模板字符串
from string import Template
#导入数据库操作
from database import *
#导入格式化数据操作
from formate import *
#导入数据存储操作
from saveFile import *
#网页基础地址
base_url = 'http://movie.mtime.com/'
#网页接口地址
api_url = Template("http://front-gateway.mtime.com/mtime-search/search/unionSearch2?keyword=&pageIndex=${pageIndex}&pageSize=20&searchType=0&locationId=290&genreTypes=&area=&year=${year}")
#获取当前时间戳
current_milli_time = lambda: int(round(time.time() * 1000))
#获取接口数据
def api(method:str,url:str)->dict:
    pool = urllib3.PoolManager()
    r = pool.request(method,url)
    #将返回的数据解码为utf-8格式,并将json数据转化为python数据
    api_data = json.loads(r.data.decode("utf-8"))
    return api_data
#获取一页的电影链接
def data_list(pageIndex:int,year:int)->list:
    url = api_url.substitute(pageIndex=pageIndex,year=year)
    api_data = api("POST",url)
    movie_data = api_data['data']
    arr = movie_data["movies"]
    return arr
#获取每部电影的id
def id_list(arr:list)->dict:
    idList = []
    for i in arr:
        name = i['name']
        id = i['movieId']
        idData = {
            'name':name,
            'id':id
            }
        idList.append(idData)
    return idList
#获取电影的详细信息
def detail(id:int)->dict:
    #拼接接口链接
        url = 'http://front-gateway.mtime.com/library/movie/detail.api?tt='+ str(current_milli_time()) + '&movieId=' + str(id) +'&locationId=290'
       
        api_data = api("GET",url)
        api_data = api_data["data"]["basic"]
        #电影名
        name = api_data["name"]
        #评分 
        grade = api_data["overallRating"] 
        #类型
        types = api_data["type"] if api_data['type'] else ''
        #主演
        actors = [i["name"]  for i in api_data["actors"]]
        #导演 
        director = api_data['director']['name'] if api_data['director'] else ''
        #国家
        countries = [i["name"] for i in api_data["countries"]]
        #时长
        mins = api_data["mins"][0:-2]
        #简介
        story = api_data["story"]
        #上映时间
        releaseDate = api_data["releaseDateNew"] if api_data["releaseDateNew"] else '1990年1月1日'
        return {
            "name":name,
            "grade":grade,
            "types":types,
            "actors":actors,
            "director":director,
            "countries":countries,
            "mins":mins,
            "story":story,
            "releaseDate":releaseDate
        }
#获取评论数据
def comment(id:int,pageIndex:int)->str:
    url = 'http://front-gateway.mtime.com/library/movie/comment.api?tt=' + str(current_milli_time()) +'&movieId=' + str(id) +'&pageIndex=' + str(pageIndex)+ '&pageSize=20&orderType=1' 
    api_data = api("GET",url)
    comment_list = api_data['data']['list']   
    comment_str = ''
    for i in comment_list:
        comment_str += i['content'] 
    return comment_str
#将详情以及评论存储
def saveFile(dataList:list,year:int):
    #获取电影id列表
    idList = id_list(dataList)
    for j in idList:
        try:
            #获取电影详细信息
            detailInfo = detail(j['id'])
            #对信息进行格式化
            data = formate(detail_data=detailInfo)
            #往数据库中添加数据
            add_detail(data,year=year)
            if(not os.path.exists('D:\\Graduation-Project\\serve\\static\\comment\\' + j['name'] + '.txt')):
                #用于拼接评论字符串
                comment_str = ''
                     #循环获取所有的评论数据
                #用于控制循环
                t = 1
                while(1):
                #暂停0.1秒后继续
                    time.sleep(0.1)
                    try:
                        cstr = comment(j["id"],pageIndex=t)
                        #没有评论，直接终止循环
                        if cstr == '':
                            break
                        comment_str += cstr + '\n'
                        t += 1
                    except Exception as e:
                        print("存储评论出错",e)
                        break               
            #如果不为空将评论存储
                if(comment_str):
                    print("ok")
                    save_comment(j['name'],comment_str)
        except Exception as e:
                print(e)
                print("出错",j)
                continue
def main(year:int):
    #创建一个存储电影详情的表
    create_detail_table(year=year)
    i = 1
    while(1):    
        try:
            dataList =  data_list(i,year=year)
            saveFile(dataList=dataList,year=year)
            i += 1
        except Exception as e:
            #如果出错直接开始下一次循环
            print(e)
            print("出错了",i)
            i += 1
            continue
        
        #如果长度小于20则到了最后一页,跳出循环
        if (len(dataList) < 20):
            break   